import java.util.Arrays;

public class MyList_0221 {
    //存放元素的数组
    private int[] elem;
    //有效元素个数
    private int usedSize;
    //初始容量
    private static final int DEFAULT_SIZE = 2;
    public MyList_0221() {
        this.elem = new int[DEFAULT_SIZE];
    };
    public MyList_0221(int ininCapacity) {
        this.elem = new int[ininCapacity];
    };
    //打印顺序表
    public void display () {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.elem[i] + ", ");
        }
        System.out.println();
    }
    //检查顺序表是否满了
    public Boolean isFull () {
        if(usedSize == this.elem.length)
            return true;
        return false;
    }
    //扩容
    public void grow () {
        this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
    }
    //数组末尾增添元素
    public void add (int data) {
        if(isFull())
            grow();
        this.elem[usedSize] = data;
        usedSize++;
    }
    //在pos位新增元素
    public void add (int pos, int data) {
        if(isFull())
            grow();
        if(pos < 0 || pos > this.usedSize) {
            throw new PosOutException(pos + "不在范围内");
        }
        for (int i = usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    //检查是否包含某一元素
    public Boolean contains (int data) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i] == data)
                return true;
        }
        return false;
    }
    //返回元素下标
    public int indexOf (int data) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i] == data)
                return i;
        }
        return -1;
    }
    //返回指定下标对应的元素
    public int get (int pos) {
        if (pos < 0 || pos >= usedSize)
            throw new PosOutException(pos + "不在范围之内");
        return this.elem[pos];
    }
    //设置某一下标为指定元素
    public void set (int pos, int data) {
        if (pos < 0 || pos >= usedSize)
            throw new PosOutException(pos + "不在范围之内");
        this.elem[pos] = data;
    }
    //删除指定位置元素
    public void remove (int pos) {
        if (pos < 0 || pos >= usedSize)
            throw new PosOutException(pos + "不在范围之内");
        for (int i = pos; i < usedSize - 1; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
    }
    //返回有效元素个数
    public int size () {
        return this.usedSize;
    }
    //清空顺序表
    public void clear () {
        this.usedSize = 0;
    }
}
